package com.muh.rabbitmq;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class CallBackProducer {
    private static final String HOST_NAME = "39.104.74.101";
    private static final int PORT = 5672;
//    private static final String QUEUE_NAME = "test3";//队列名称
    private static final String USER_NAME = "admin";
    private static final String PASSWORD = "admin";
    private static final String VIRTUAL_HOST = "/mirror";//虚拟机

//    private static final String EXCHANGE_NAME = "callbackExchange";//交换机

    private static final String EXCHANGE_NAME = "callbackExchange";
    private static final String ALTER_EXCHANGE_NAME = "alterExchange";
    private static final String QUEUE_NAME = "callbackQueue";


    public static void main(String[] args) throws Exception {
        //建立连接Factory
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(HOST_NAME);
        factory.setPort(PORT);
        factory.setUsername(USER_NAME);
        factory.setPassword(PASSWORD);
        factory.setVirtualHost(VIRTUAL_HOST);

        //建立连接
        Connection connection = factory.newConnection();
        //创建一个通道用来传输数据
        Channel channel = connection.createChannel();

        Map<String, Object> exchangeDeclareParams = new HashMap<String, Object>();
        exchangeDeclareParams.put("alternate-exchange", ALTER_EXCHANGE_NAME);
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true, false, exchangeDeclareParams);
        channel.exchangeDeclare(ALTER_EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true, false, exchangeDeclareParams);

        //申明一个对列
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);

        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "key1");

        channel.addConfirmListener(new ConfirmListener() {
            @Override
            public void handleAck(long deliverTag, boolean multiple) throws IOException {
                System.out.println("message acked; deliverTag: " + deliverTag);
            }
            @Override
            public void handleNack(long deliverTag, boolean multiple) throws IOException {
                System.out.println("message Nacked; deliverTag: " + deliverTag);
            }
        });

        channel.addReturnListener(new ReturnListener() {
            @Override
            public void handleReturn(int replyCode, String replyText, String s1, String s2,
                                     AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("message returned; replyCode: " + replyCode+";replytext:"+replyText+";s1:"+s1+";s2:"+s2+";message:"+new String(body, "UTF-8")+";props:"+properties);
            }
        });
        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder();
        builder.deliveryMode(MessageProperties.PERSISTENT_TEXT_PLAIN.getDeliveryMode());
        builder.priority(MessageProperties.PERSISTENT_TEXT_PLAIN.getPriority());
        builder.correlationId("111");

        String message = " aaaaa";
        channel.basicPublish(EXCHANGE_NAME, "key1",true, builder.build(), message.getBytes());
        channel.basicPublish(EXCHANGE_NAME, "key2",true, builder.build(), message.getBytes());
        System.out.println("message sent");

        Thread.sleep(5000);
        channel.close();
        connection.close();
    }
}
